// pages/datacenter/datacenter.js
const util = require('../../utils/util.js')
import basesup from '~/utils/supplier.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentDate: new Date().getTime(),
        minDate: '',
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            if (type === 'day') {
                return `${value}号`;
            }
            return value;
        },
        show1: false,
        show2: false,
        selectdate1: '请选择时间',
        selectdate2: "请选择时间",
        currentDate1: '',
        currentDate2: '',

        topdata:[],
        bottomdata:[],
    },

    
    navedatadetails(items) {
        console.log(items)
        wx.navigateTo({
            url: `../datadetails/datadetails?select=${items.currentTarget.dataset.select}`,
        })
    },
    openshow1() {
        this.setData({
            show1: true
        });
    },
    openshow2() {
        this.setData({
            show1: true
        });
    },
    onClose() {
        this.setData({
            show1: false,
            show2: false,
        });
    },
    onInput1(event) {
        console.log(util.formatDate(event.detail))
        this.setData({
            currentDate1: event.detail,
            selectdate1: util.formatDate(event.detail)
        });
        this.onClose()
    },
    onInput2(event) {
        console.log(util.formatDate(event.detail))
        this.setData({
            currentDate2: event.detail,
            selectdate2: util.formatDate(event.detail)
        });
        this.onClose()
    },

    async gattopdata() {
        let str = this.data.selectdate1;
        str = str.replace(/年|月/g, '-').replace(/日/g, '');
        let [year, month, day] = str.split('-');
        month = month.length === 1 ? '0' + month : month;
        day = day.length === 1 ? '0' + day : day;
        const formattedStr = `${year}-${month}-${day}`;

        let reqdata = {
            time: formattedStr
        }
        let resdata = await basesup.threeCountsample(reqdata)
        console.log(resdata)
        if (resdata.code == 200) {
            this.setData({
                topdata:resdata.data.data.items
            })
        } else {
            wx.showToast({
                title: '获取失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
        this.onClose()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let lookdata = util.formatDate(new Date().getTime())
        this.setData({
            selectdate1: lookdata,
            selectdate2: lookdata,
        })
        this.gattopdata()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})